package com.contactmgr.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

import com.contactmgr.logObjects.Sms;

public class SmsReader {

	final static String ID = "_id";
	final static String ADDRESS = "address";
	final static String PERSON = "person";
	final static String DATE = "date";
	final static String BODY = "body";

	// TODO: change to CONTENT URI
	public static List<Sms> readInbox(ContentResolver cr) {
		Uri uriSMSURI = Uri.parse("content://sms/inbox");
		Cursor cur = cr.query(uriSMSURI, null, null, null, null);
		List<Sms> msgList = new ArrayList<Sms>();
		while (cur.moveToNext()) {
			Sms msg = readSms(cur);
			msg.setSmsType(Sms.TYPE.RECEIVED);
			msgList.add(msg);
		}
		return msgList;
	}

	private static Sms readSms(Cursor cur) {

		Sms msg = new Sms();
		msg.setFromTo(cur.getString(cur.getColumnIndex(ADDRESS)));
		msg.setContent(cur.getString(cur.getColumnIndex(BODY)));
		msg.setDate(cur.getString(cur.getColumnIndex(DATE)));
		System.out.println(msg.toString());
		return msg;
	}

	public static List<Sms> readSentItems(ContentResolver cr) {
		Uri uriSMSURI = Uri.parse("content://sms/sent");
		Cursor cur = cr.query(uriSMSURI, null, null, null, null);
		List<Sms> msgList = new ArrayList<Sms>();
		while (cur.moveToNext()) {
			Sms msg = readSms(cur);
			msg.setSmsType(Sms.TYPE.SENT);
			msgList.add(msg);
		}
		return msgList;
	}

}
